﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using System.Text.RegularExpressions;
using Log;
namespace PayManage
{
    static class Program
    {
        //这里第2个参数是保证你程序唯一性的而一个字符串
        static Mutex mutex = new Mutex(false, "f880a127-9999-4907-aa83-6041dc0faa4a");

        private static readonly ILog logger = LogManager.GetLogger(typeof(Program));
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (!mutex.WaitOne(300, false)) //等待5秒, 如果有相同实例运行则给用户提示
            {
                MessageBox.Show("程序已在运行，如果仍有问题，请检查是否已在系统进程中运行。");
                return;
            }
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                if (new frmLogin().ShowDialog().Equals(DialogResult.Yes))
                {
                    if (DataService.IsLogin)
                    {
                        Application.Run(new frmMain());
                    }
                    else
                    {
                        Application.Exit();
                    }
                }                
                /*
                string method = "'BasicPay'+'JobPayAdd'";
                Dictionary<string, int> dic = new Dictionary<string, int>();
                dic.Add("BasicPay", 500);
                dic.Add("JobPayAdd", 21);
                method = Regex.Replace(method, @"'[A-Za-z0-9]*'", (e) => dic[e.Value.Trim('\'').ToString()].ToString());              
                object ret = strUtil.qswhEval1(method);
                MessageBox.Show(ret.ToString());
                * */
            }
            finally
            {
                DataService.Save();
                //DataService.SaveSheets();
                mutex.ReleaseMutex();
                Application.ExitThread();
            }

        }

        static void Test()
        {
            //DataService.GetCellValue(@"d:\long\aa.xls", "管理人员", 9,"N");

                  //string summethod = "=SUM(O4:O30)";         
                  //  string[] temp = summethod.Split(':');
                  //  summethod = Regex.Replace(temp[0], "[^\\d]\\d+[^\\d]", "4") + ":" + Regex.Replace(temp[1], "[^\\d]\\d+[^\\d]", "20");
                  //  Console.Write(summethod);

            //扣伙食，memos：外宿,扣100元,,|,,,|外宿,,,

            Console.WriteLine("测试从文字里提取扣伙食，请输入如：：外宿,扣100元,,|,,,|外宿,,,.如果想退出，请输入[exit]");
            while (true)
            {
                string memo = Console.ReadLine();
                if (memo == null)
                    continue;
                if (memo.ToLower().Equals("exit"))
                    break;
                double DelEat = 0;
                memo = Regex.Replace(memo, @"[^扣\d+]", "").Replace("扣", "");
                if (!string.IsNullOrEmpty(memo))
                {
                    DelEat = cvt.ToDouble(memo);
                }
                Console.Write(string.Format("memo:{0},DelEat:{1}", memo, DelEat));
            }
         
            
        }
    }   
}
